package cc.fs.zhihuapp.ui.activity.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;

/**
 * Created by fostion on 2015/10/9.
 * 测试使用leakcanary检测内存泄露
 */
public class LeakCanaryActivity extends BaseActivity {

    public final static String ACTION_EXIT_APP = "cc.fs.zhihuapp";
    private static LocalBroadcastManager mLocalBroadcatManager;
    private BroadcastReceiver mExitReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_EXIT_APP)) {
                Log.d("TAG", "exit from broadcast");
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        init();

    }

    private void init() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_EXIT_APP);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        getLocalBroadcastManager().registerReceiver(mExitReceiver, filter);
    }

    private  LocalBroadcastManager getLocalBroadcastManager() {
        if (mLocalBroadcatManager == null) {
            mLocalBroadcatManager = LocalBroadcastManager.getInstance(this);
        }
        return mLocalBroadcatManager;
    }

    @Override
    public void handler(Message msg) {

    }
}
